//
//  MensaListController.m
//  RegBars
//
//  Created by Huu Nhat on 9/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MensaListController.h"
#import "RegBarsAppDelegate_iPhone.h"
#import "MensaCell.h"
#import "DateTimeHelper.h"
#import "CitiesViewController.h"

@implementation MensaListController
@synthesize mensaCell;
@synthesize mensaTable;
@synthesize titleView;
@synthesize leftView;
@synthesize centerView;
@synthesize rightView;
@synthesize titleBarBackground;
@synthesize titleLabel;
@synthesize previousButton;
@synthesize nextButton;
@synthesize loadingView;
@synthesize viewArrays;
@synthesize plansArray;
@synthesize headerSection1;
@synthesize headerSection2;
@synthesize headerSection3;
@synthesize headerSection4;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    firstTime = YES;
    [super viewDidLoad];
	plansArray = [[NSMutableArray alloc] init];
	vorspeisenArray = [[NSMutableArray alloc] init];
	hauptspeisenArray = [[NSMutableArray alloc] init];
	nachtischArray = [[NSMutableArray alloc] init];
	suppeArray = [[NSMutableArray alloc] init];
	[self.view addSubview:leftView];
    [leftView setFrame:CGRectMake(-320, 44, 320, 367)];
    
    [self.view addSubview:rightView];
    [rightView setFrame:CGRectMake(320, 44, 320, 367)];
    
    [self.view addSubview:centerView];
    [centerView setFrame:CGRectMake(0, 44, 320, 367)];
	
    viewArrays = [[NSMutableArray alloc] initWithObjects:leftView, centerView,rightView, nil];
    [self.view addSubview:loadingView];
    [loadingView setFrame:CGRectMake(0, 44, 320, 367)];
    NSLog(@"mainAppDelegate.currentDayValue = %d",mainAppDelegate.currentDayValue);
    if (mainAppDelegate.currentDayValue == 6) {
        mainAppDelegate.currentDayValue = 7;
    }
	[self updatePlan:mainAppDelegate.currentDayValue];
}

- (IBAction)backToCity:(id)sender
{
    //[mainAppDelegate_iPhone.citiesViewController flipToHome];
    
}

- (void) updatePlan:(int) dateValue{
	NSLog(@"dateValue %d",dateValue);
    switch (dateValue) {
        case 2:
            [plansArray setArray:mainAppDelegate.mondayPlans];
            break;
        case 3:
            [plansArray setArray:mainAppDelegate.tuesdayPlans];
            break;
        case 4:
            [plansArray setArray:mainAppDelegate.wednesdayPlans];
            break;
        case 5:
            [plansArray setArray:mainAppDelegate.thursdayPlans];
            break;
        case 6:
            [plansArray setArray:mainAppDelegate.fridayPlans];
            break;
        case 7:
            [plansArray setArray:mainAppDelegate.nextmondayPlans];
            break;
        case 8:
            [plansArray setArray:mainAppDelegate.nexttuesdayPlans];
            break;
        case 9:
            [plansArray setArray:mainAppDelegate.nextwednesdayPlans];
            break;
        case 10:
            [plansArray setArray:mainAppDelegate.nextthursdayPlans];
            break;
        case 11:
            [plansArray setArray:mainAppDelegate.nextfridayPlans];
            break;
    }
	if ([plansArray count]!=0) {
		Week *week = [plansArray objectAtIndex:0];
		[titleLabel setText:[NSString stringWithFormat:@"%@,%@",week.day, week.date]];
	}
	[self separatePlans];
}

- (void) separatePlans{
	[vorspeisenArray removeAllObjects];
	[hauptspeisenArray removeAllObjects];
	[nachtischArray removeAllObjects];
	[suppeArray removeAllObjects];
	for (Week *week in plansArray) {
		if ([week.goodGroup isEqualToString:@"B1"]||[week.goodGroup isEqualToString:@"B2"]||[week.goodGroup isEqualToString:@"B3"]||[week.goodGroup isEqualToString:@"B4"]||[week.goodGroup isEqualToString:@"B5"]||[week.goodGroup isEqualToString:@"B6"]) {
			[vorspeisenArray addObject:week];
		} else if ([week.goodGroup isEqualToString:@"HG1"]||[week.goodGroup isEqualToString:@"HG2"]||[week.goodGroup isEqualToString:@"HG3"]||[week.goodGroup isEqualToString:@"HG4"]||[week.goodGroup isEqualToString:@"HG5"]||[week.goodGroup isEqualToString:@"HG6"]) {
			[hauptspeisenArray addObject:week];
		} else if ([week.goodGroup isEqualToString:@"N1"]||[week.goodGroup isEqualToString:@"N2"]||[week.goodGroup isEqualToString:@"N3"]||[week.goodGroup isEqualToString:@"N4"]||[week.goodGroup isEqualToString:@"N5"]||[week.goodGroup isEqualToString:@"N6"]) {
			[nachtischArray addObject:week];
		} else if ([week.goodGroup isEqualToString:@"Suppe"]) {
			[suppeArray addObject:week];
		}
	}
	[mensaTable reloadData];
	[loadingView setHidden:YES];
}

- (IBAction)previousButtonPress{
	[loadingView setHidden:NO];
	if (mainAppDelegate.currentDayValue == 2) {
		mainAppDelegate.currentDayValue = 11;
	} else {
		mainAppDelegate.currentDayValue--;
	}
	[self showPreviosView];
	[self updatePlan:mainAppDelegate.currentDayValue];
}

- (IBAction)nextButtonPress{
	[loadingView setHidden:NO];
	if (mainAppDelegate.currentDayValue == 11) {
		mainAppDelegate.currentDayValue = 2;
	} else {
		mainAppDelegate.currentDayValue++;
	}
	[self showNextView];
	[self updatePlan:mainAppDelegate.currentDayValue];
}

- (void) showNextView{
    UIView *center = (UIView*)[viewArrays objectAtIndex:1];
    UIView *right = (UIView*)[viewArrays objectAtIndex:2];
    
    [UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	CGRect frame = [center frame];
	frame.origin.x -= 320;
	[center setFrame:frame];
	[UIView commitAnimations];
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	CGRect frameAnimate = [right frame];
	frameAnimate.origin.x += 320;
	[right setFrame:frameAnimate];
	[UIView commitAnimations];
    
    [self performSelector:@selector(updateRight) withObject:nil afterDelay:1];
}

- (void) showPreviosView
{
    UIView *left = (UIView*)[viewArrays objectAtIndex:0];
    UIView *center = (UIView*)[viewArrays objectAtIndex:1];
    
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	CGRect frame = [center frame];
	frame.origin.x += 320;
	[center setFrame:frame];
	[UIView commitAnimations];
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.3f];
	CGRect frameAnimate = [left frame];
	frameAnimate.origin.x -= 320;
	[left setFrame:frameAnimate];
	[UIView commitAnimations];
    
    [self performSelector:@selector(updateLeft) withObject:nil afterDelay:1];
}

- (void) layout{
    UIView *left = (UIView*)[viewArrays objectAtIndex:0];
    [left setFrame:CGRectMake(-320, 44, 320, 416)];
    UIView *center = (UIView*)[viewArrays objectAtIndex:1];
    [center setFrame:CGRectMake(0, 44, 320, 616)];
    UIView *right = (UIView*)[viewArrays objectAtIndex:2];
    [right setFrame:CGRectMake(320, 44, 320, 416)];
    [mensaTable removeFromSuperview];
    [center addSubview:mensaTable];
    [mensaTable setFrame:CGRectMake(0, 0, 320, 367)];
}

- (void) updateLeft{
    UIView *left = (UIView*)[viewArrays objectAtIndex:0];
    UIView *center = (UIView*)[viewArrays objectAtIndex:1];
    UIView *right = (UIView*)[viewArrays objectAtIndex:2];
    [viewArrays removeAllObjects];
    [viewArrays insertObject:center atIndex:0];
    [viewArrays insertObject:right atIndex:1];
    [viewArrays insertObject:left atIndex:2];
    [self layout];
}

- (void) updateRight{
    UIView *left = (UIView*)[viewArrays objectAtIndex:0];
    UIView *center = (UIView*)[viewArrays objectAtIndex:1];
    UIView *right = (UIView*)[viewArrays objectAtIndex:2];
    [viewArrays removeAllObjects];
    [viewArrays insertObject:right atIndex:0];
    [viewArrays insertObject:left atIndex:1];
    [viewArrays insertObject:center atIndex:2];
    [self layout];
}

#pragma mark -
#pragma mark UITableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	if (section == 0) {
		return [suppeArray count];
	}
    if (section == 1) {
		return [vorspeisenArray count];
	}
	if (section == 2) {
		return [hauptspeisenArray count];
	}
	if (section == 3) {
		return [nachtischArray count];
	}
	return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
	return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
	if (section == 0) {
		return headerSection1;
	}
	if (section == 1) {
		return headerSection2;
	}
	if (section == 2) {
		return headerSection3;
	}
	if (section == 3) {
		return headerSection4;
	}
	
	return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *cellId = @"MensaCellId";
	mensaCell = (MensaCell*)[tableView dequeueReusableCellWithIdentifier:cellId];
	if (mensaCell == nil) {
		[[NSBundle mainBundle] loadNibNamed:@"MensaCell" owner:self options:nil];
	}
	if (indexPath.section == 0) {
		if ([suppeArray count] == 1) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"one_cell_bg.png"]];
		} else if (indexPath.row == 0) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"first_cell_bg.png"]];
		} else if (indexPath.row == ([vorspeisenArray count] - 1) ) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"last_cell_bg.png"]];
		} else {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"middle_cell_bg.png"]];
		}
		Week *week = [suppeArray objectAtIndex:indexPath.row];
		mensaCell.nameLabel.text = week.name;
		mensaCell.nameLabel.text = [NSString stringWithFormat:@"%@ - EUR %@",week.name,[[week.price componentsSeparatedByString:@"/"] objectAtIndex:0]];
	}
	if (indexPath.section == 1) {
		if ([vorspeisenArray count] ==1) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"one_cell_bg.png"]];
		} else if (indexPath.row == 0) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"first_cell_bg.png"]];
		} else if (indexPath.row == ([vorspeisenArray count] - 1) ) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"last_cell_bg.png"]];
		} else {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"middle_cell_bg.png"]];
		}
		Week *week = [vorspeisenArray objectAtIndex:indexPath.row];
		mensaCell.nameLabel.text = week.name;
		mensaCell.nameLabel.text = [NSString stringWithFormat:@"%@ - EUR %@",week.name,[[week.price componentsSeparatedByString:@"/"] objectAtIndex:0]];
	}
	if (indexPath.section == 2) {
		if ([hauptspeisenArray count] ==1) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"one_cell_bg.png"]];
		} else if (indexPath.row == 0) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"first_cell_bg.png"]];
		} else if (indexPath.row == ([hauptspeisenArray count]-1)) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"last_cell_bg.png"]];
		} else {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"middle_cell_bg.png"]];
		}
		Week *week = [hauptspeisenArray objectAtIndex:indexPath.row];
		mensaCell.nameLabel.text = week.name;
		mensaCell.nameLabel.text = [NSString stringWithFormat:@"%@ - EUR %@",week.name,[[week.price componentsSeparatedByString:@"/"] objectAtIndex:0]];
	}
	if (indexPath.section == 3) {
		if ([nachtischArray count] ==1) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"one_cell_bg.png"]];
		} else if (indexPath.row == 0) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"first_cell_bg.png"]];
		} else if (indexPath.row == ([nachtischArray count] - 1)) {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"last_cell_bg.png"]];
		} else {
			[mensaCell.backgroundImage setImage:[UIImage imageNamed:@"middle_cell_bg.png"]];
		}
		Week *week = [nachtischArray objectAtIndex:indexPath.row];
		mensaCell.nameLabel.text = [NSString stringWithFormat:@"%@ - EUR %@",week.name,[[week.price componentsSeparatedByString:@"/"] objectAtIndex:0]];
	}
	[mensaCell.nameLabel setTextColor:[UIColor blueColor]];
	return [mensaCell autorelease];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didNotRecieveCSVData{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Please check your network connection" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.mensaTable = nil;
	self.titleView = nil;
	self.leftView = nil;
	self.centerView = nil;
	self.rightView = nil;
	self.titleBarBackground = nil;
	self.titleLabel = nil;
	self.previousButton = nil;
	self.nextButton = nil;
	self.loadingView = nil;
	self.mensaCell = nil;
	self.headerSection1 = nil;
	self.headerSection2 = nil;
	self.headerSection3 = nil;
	self.headerSection4 = nil;
}


- (void)dealloc {
	self.mensaCell = nil;
	self.mensaTable = nil;
	self.titleView = nil;
	self.leftView = nil;
	self.centerView = nil;
	self.rightView = nil;
	self.titleBarBackground = nil;
	self.titleLabel = nil;
	self.previousButton = nil;
	self.nextButton = nil;
	self.loadingView = nil;
	self.headerSection1 = nil;
	self.headerSection2 = nil;
	self.headerSection3 = nil;
	self.headerSection4 = nil;
	[viewArrays release];
	[plansArray release];
    [super dealloc];
}


@end
